package com.airS.task.fileMgr.controller.base;

import com.airS.task.fileMgr.common.constants.FilemgrConstants;
import com.airS.task.fileMgr.mapper.extra.gk.PermissionExtraMapper;
import com.airS.task.fileMgr.model.gk.Permission;
import com.airS.task.fileMgr.model.gk.PermissionTree;
import com.airS.task.fileMgr.security.shiro.realm.SystemAuthorizingRealm;
import com.airS.task.fileMgr.utils.shiro.SecurityUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by alan on 2017/3/15.
 */
public abstract class SupportController {

    private static Logger logger = LoggerFactory.getLogger(SupportController.class);

    @Autowired
    private PermissionExtraMapper permissionExtraMapper;

    private static final String PERMISSION_TREES = "permissionTrees";

    @ModelAttribute
    public void initMenuTree(HttpSession session){
        SystemAuthorizingRealm.Principal principal = SecurityUtils.getPrincipal();
        Object obj = session.getAttribute(PERMISSION_TREES);
        if(session.getAttribute(PERMISSION_TREES) == null && principal != null){
            // 查询当前用户菜单结果集
            List<Permission> permissions = permissionExtraMapper.selectByExample(principal.getEmployeeId());
            // 判断是否二次添加
            Set<Integer> infiniteLoop = new HashSet<Integer>();
            // 组装菜单树,前端只迭代两层
            List<PermissionTree> permissionTrees = assemblyMenuTree(permissions, 0, infiniteLoop);
            session.setAttribute(PERMISSION_TREES, permissionTrees);
        }
    }

    /**
     * 组装菜单树
     * @param allPermission
     * @param parentId
     * @param infiniteLoop
     * @return
     */
    private List<PermissionTree> assemblyMenuTree(List<Permission> allPermission, Integer parentId, Set<Integer> infiniteLoop){
        List<PermissionTree> permissionTrees = new ArrayList<PermissionTree>();
        // 迭代所有权限
        for(Permission permission : allPermission){
            // 当前父权限ID等于parentId并且是非功能性菜单时才组装
            if(permission.getParentId().compareTo(parentId) == 0 && FilemgrConstants.COMMON_BOOLEAN.B0F.equals(permission.getIsFunction())){
                PermissionTree permissionTree = new PermissionTree();
                try {
                    // 复制
                    BeanUtils.copyProperties(permissionTree, permission);
                } catch (Exception e) {
                    logger.error("首页菜单组装异常", e);
                    return null;
                }
                // 判断是否出现死循环
                if(infiniteLoop.contains(permissionTree.getPermissionId())){
                    logger.error("首页菜单组装异常，出现循环依赖");
                    return null;
                }
                // 添加,并递归调用
                permissionTrees.add(permissionTree);
                infiniteLoop.add(permissionTree.getPermissionId());
                permissionTree.setChildPermissionTrees(assemblyMenuTree(allPermission, permissionTree.getPermissionId(), infiniteLoop));
            }
        }
        return permissionTrees;
    }

}
